<?php
/**
* Validation Class
* v3.0
* Last Updated: Mar 28, 2011
* URL: http://www.nickyeoman.com/blog/php/104-php-validation-class
*
* Changelog:
* v2 now works with PHP 5.3 and up
* v3 is easy to intergrate into CI as a library (renamed) + bug fixes
**/
 
class Validator {
    
    private $mc_activ;
    private $mysqli_res_exists;
    private $mysql_res_exists;
    
    function __construct() {
        $this->mc_active = get_magic_quotes_gpc();
        $this->mysqli_res_exists = function_exists("mysqli_real_escape_string");
        $this->mysql_res_exists = function_exists("mysql_real_escape_string");
    }


    public function escape_value ( $value ) {
        global $db;
        
        if ( is_string($value) )  {
            $value = $value;
            if ( $this->mysqli_res_exists ) {
                // Check that mysqli_real_escape_string is available
                // PHP 5+
                
                // IF Magic quotes is on.. we dont want it.. undo the slashing
                if ( $this->mc_active ) $value = stripslashes($value);
                
                return mysqli_real_escape_string( $db->the_connecttion(), $value );
                
            } elseif ( $this->mysql_res_exists ) {
                // Check for the older version, mysql_real_escape_string
                // PHP 4.3.0
                        
                // IF Magic quotes is on.. we dont want it.. undo the slashing
                if ( $this->mc_active ) $value = stripslashes( $value);
                
                return mysql_real_escape_string( $value );
                
            } elseif ( !$this->mc_active ) {
                // if Magic quotes is not active, add in slashes
                return  addslashes( $value );
            
            }
        
            // If this is really happening... let Magic Quotes do it's thing
            return $value;
        } if ( is_int( $value ) || is_array($value)){
            return $value;
        }
        
    }


  function is_email($email, $required=FALSE) {

    $email =  strtolower($email);
 
    if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email)) {
      return $email;
    }
 
  }
    /**
     * 
     * This function prepares the CSV string for insertion.
     * 
     * 
     */
    public function prepare_csv( $csv ) {
        
        $array = explode(",",$csv);
        foreach( $array as $k => $v ) {
            $v = trim($v);
            if( !empty( $v ) ) {
                $array[$k] = $this->escape_value($v);
            } else {
                unset( $array[$k]);
            }
        }
        
        return implode(',', $array);
    }
    
    
 
 
 
  public function escape_class(&$class) {
        // Uses a reference of this class to update its values.
        foreach($class as $key => $value) {
            $class->$key = $this->escape_value($value);   
        }
  }
  /**
  * Checks if there are 7 or 10 numbers, if so returns cleaned parameter(no formating just numbers)
  * other wise it will return false 
  * $phone is the phone number
  * $ext if set to true return an array with extension separated
  **/
  function is_phone($phone, $ext = false) {
 
    //remove everything but numbers
    $numbers = preg_replace("%[^0-9]%", "", $phone );
    
    //how many numbers are supplied
    $length = strlen($numbers);
 
    if ( $length == 10 || $length == 7 ) { //Everything is find and dandy
 
      $cleanPhone = $numbers;
 
      if ( $ext ) {
        $clean['phone'] = $cleanPhone;
        return $clean;
      } else {
        return $cleanPhone;
      }
 
    } elseif ( $length > 10 ) { //must be extension
 
      //checks if first number is 1 (this may be a bug for you)
      if ( substr($numbers,0,1 ) == 1 ) {
        $clean['phone'] = substr($numbers,0,11);
        $clean['extension'] = substr($numbers,11);
      } else {
        $clean['phone'] = substr($numbers,0,10);
        $clean['extension'] = substr($numbers,10);
      }
 
      if (!$ext) { //return string
 
        if (!empty($clean['extension'])) {
          $clean = implode("x",$clean);
        } else {
          $clean = $clean['phone'];
        } 
 
        return $clean;
 
 
      } else { //return array
 
        return $clean;
      }
    } 
 
    return false;
 
  }
 
  /**
  * Canadian Postal code
  * thanks to: http://roshanbh.com.np/2008/03/canda-postal-code-validation-php.html
  **/
  function isPostalCode($postal) {
    $regex = "/^([a-ceghj-npr-tv-z]){1}[0-9]{1}[a-ceghj-npr-tv-z]{1}[0-9]{1}[a-ceghj-npr-tv-z]{1}[0-9]{1}$/i";
 
    //remove spaces
    $postal = str_replace(' ', '', $postal);
 
    if ( preg_match( $regex , $postal ) ) {
      return $postal;
    } else {
      return false;
    }
 
  }
 
  /** 
  * Checks for a 6 digit post code
  * Clears extra characters
  * returns clean postal code
  **/
  function isZipCode($zip) {
    //remove everything but numbers
    $numbers = preg_replace("[^0-9]", "", $zip );
 
    //how many numbers are supplied
    $length = strlen($numbers);
 
    if ($length != 5) {
      return false;
    } else {
      return $numbers;
    }
  }
  
  



    
    public function is_name ($string) {
        if( $string = $this->_clean_var($string)) {
            $string = filter_var($string, FILTER_SANITIZE_STRING);
            if ( !preg_match("/[^a-zA-Z- ]/", $string ) ) return $string;
        } 
        return false;
    }
    
    private function _clean_var($data){
        global $session;
        if ( $data  ) {
            $data = trim( $data );
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        } else {
            return false;
        }
    }
    private function _check_var_for_html($string){
        if ( preg_match("/([\<])([^\>]{1,})*([\>])/i", $string) ) {
            return false;
        } else {
            return $string;
        }
    } 
    
    
    
    public function is_valid ($string) {
       if ( $string = $this->_check_var_for_html($string) ) {
           $string = $this->_clean_var($string);       
          if( !empty($string) ) {
               $string = filter_var($string, FILTER_SANITIZE_STRING);
                return $string;
          }
       } 
        return false;
   }   
    
    
}